package GUI.dao.impl;

import GUI.dao.BaseDAO;
import GUI.dao.UserDAO;
import GUI.pojo.UserDO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//JUnit单元测试
public class UserDAOImpl extends BaseDAO implements UserDAO {
    @Override
    public UserDO getByUsernameAndPwd(String username, String pwd) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_name=? and u_password=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            statement.setObject(2, pwd);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setAccount(rs.getString("u_name"));
                user.setPwd(rs.getString("u_password"));
                user.setBalance(rs.getDouble("u_balance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }

    @Override
    public void update(UserDO user) {
        // 1. 创建连接
//        Connection con = getConnection();
//        PreparedStatement statement = null;
        this.getConnection();
        String sql = "update t_user set u_password=?,u_balance=? where u_id=?";
        try {
            // 2. 获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setObject(1, user.getPwd());
            statement.setObject(2, user.getBalance());
            statement.setObject(3, user.getId());
            // 3. 执行SQL语句
            int row = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭连接
//            close(con,statement);
        }
    }

    @Override
    public UserDO getById(int id) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_id=?" ;
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,id);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setAccount(rs.getString("u_name"));
                user.setPwd(rs.getString("u_password"));
                user.setBalance(rs.getDouble("u_balance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }

    @Override
    public UserDO getByUsername(String username) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_name=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,username);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setAccount(rs.getString("u_name"));
                user.setPwd(rs.getString("u_password"));
                user.setBalance(rs.getDouble("u_balance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }

}
